package com.tong.hao.ac.service.controller.file;

import com.tong.hao.ac.api.model.in.file.FileIn;
import com.tong.hao.ac.api.model.out.file.FileOut;
import com.tong.hao.ac.api.service.file.IAcFileController;
import com.tong.hao.common.configuration.sftp.SftpUtil;
import com.tong.hao.common.configuration.sftp.TongSftpConfig;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;

/**
 * @ClassName AcFileController
 * @Author 友野浩二
 * @Date 2019/12/1 21:13
 * @Description 文件处理
 * @Version 1.0
 */
@Api(tags = "SFTP文件处理")
@RestController
@Slf4j
public class AcFileController implements IAcFileController {

    @Autowired
    private TongSftpConfig tongSftpConfig;

    @Autowired
    private SftpUtil sftpUtil;

    @ApiOperation(value = "SFTP文件上传")
    @RequestMapping(value = FILE_UPLOAD, method = RequestMethod.POST)
    @Override
    public FileOut upload(@RequestBody FileIn fileIn) {
        SftpUtil sftpUtil = this.sftpUtil.getSftpUtil();
        // 请求文件路径
        String reqFilePath = fileIn.getTongInHead().getReqFilePath();

        File file = new File(reqFilePath + fileIn.getFileName());

        // SFTP文件上传
        boolean sftpUploadResult = sftpUtil.upload(file, this.tongSftpConfig.getRemotePath());
        sftpUtil.release();

        FileOut fileOut = new FileOut();
        fileOut.setSftpUploadResult(sftpUploadResult);

        return fileOut;
    }
}
